//
//  BanBu_ImageTableController.m
// Halfarea
//
//  Created by Wang Xj on 14-1-8.
//
//

#import "BanBu_ImageTableController.h"
#import "BanBu_ImageCell.h"
#import "BanBu_WatermarkView.h"

@interface BanBu_ImageTableController ()

@end

@implementation BanBu_ImageTableController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.fileName = [[[[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"Caches"] stringByAppendingPathComponent:@"watermark"]stringByAppendingPathComponent:self.folderName];
    
    NSString * imageFile = [self.fileName stringByAppendingPathComponent:@"watermark-Info.plist"];
    
    NSDictionary * imageDic = [NSDictionary dictionaryWithContentsOfFile:imageFile];
    
    self.imageArray = [NSMutableArray arrayWithArray:imageDic.allValues];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)dealloc
{
    [_folderName release],_folderName = nil;
    [_imageArray release],_imageArray = nil;
    [_fileName release],_fileName = nil;
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ceilf(self.imageArray.count/2.0);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BanBu_ImageCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[BanBu_ImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if ((indexPath.row*2+2)<self.imageArray.count)
    {
        [cell initFirstWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2]]];
        [cell initSecondWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2+1]]];
        [cell initThirdWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2+2]]];
    }
    else if ((indexPath.row*2+1)<self.imageArray.count)
    {
        [cell initFirstWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2]]];
        [cell initSecondWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2+1]]];
    }
    else if((indexPath.row*2) < self.imageArray.count)
    {
        [cell initFirstWatermarkImage:[self.fileName stringByAppendingPathComponent:[self.imageArray objectAtIndex:indexPath.row*2]]];
    }
    
    return cell;
}


#pragma mark - BanBu_imageViewDelegate
-(void)didSelectedWatermarkView:(BanBu_WatermarkView *)watermarkView
{
    //选定的图片是watermarkView
    
    //1.勾选上该图片
    
    //2.响应父类
    if ([self.delegate respondsToSelector:@selector(didSelectedImage:andImagePath:andController:)])
    {
        UIImage * image = [UIImage imageWithContentsOfFile:watermarkView.imagePath];
        [self.delegate didSelectedImage:image andImagePath:watermarkView.imagePath andController:self];
    }    
}

@end
